package com.qpf.mycms.website.login.controller;

import com.qpf.mycms.api.pojo.UserRequest;
import com.qpf.mycms.website.login.service.MemberUserLoginService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private MemberUserLoginService loginService;
    @GetMapping("/login")
    public String login(Model model)  {
        return "login";
    }
    @PostMapping("/login")
    public String login(Model model, UserRequest request, HttpSession session)  {
        System.out.println(request);
        session.setAttribute("user", loginService.login(request.getUsername(), request.getPassword()));
        return "redirect:/";
    }
    @GetMapping("/logout")
    public String logout(Model model, HttpSession session)  {
        session.setAttribute("user", null);
        return "redirect:/";
    }
    @GetMapping("/register")
    public String register(Model model)  {
        return "register";
    }
    @PostMapping("/register")
    public String register(Model model, UserRequest request)  {
        loginService.register(request);
        return "redirect:/";
    }
}